package org.jeecg.modules.demo.weixin.message.utils;


import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.weixin.message.config.MassageConfig;


@Slf4j
public class MessageUtils {
    //模版固定格式
    public static JSONObject noticeMessage(String mobile ,String content) throws Exception {
        String url = MassageConfig.ddyNoticeMessage;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("apikey", MassageConfig.ddyApiKey);
        jsonObject.put("mobile", mobile);
        jsonObject.put("content", content);
        String post = HttpClientUtils.post(url, JSON.toJSONString(jsonObject), "application/json", "UTF-8", 10000, 10000);
        JSONObject jsonObject1 = JSONObject.parseObject(post);
        return jsonObject1;
    }

    //通知短信：选择模版
    public static JSONObject notice(String mobile, String content) throws Exception {
        String url = MassageConfig.ddyNoticeMessage;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("apikey", MassageConfig.ddyApiKey);
        jsonObject.put("mobile", mobile);
        jsonObject.put("content", content);
        String post = HttpClientUtils.post(url, JSON.toJSONString(jsonObject), "application/json", "UTF-8", 10000, 10000);
        JSONObject jsonObject1 = JSONObject.parseObject(post);
        return jsonObject1;
    }

    public static Boolean yzmMessage(String phone, JSONObject templateParamJson) throws Exception {
        String s = RandomUtil.randomNumbers(6);
        log.info("-------验证码信息" + s);
        String url = MassageConfig.ddyYZMMessages;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("apikey", MassageConfig.ddyApiKey);
        jsonObject.put("mobile", phone);
        String replace = MassageConfig.ddyContents.replace("{验证码}", (String) templateParamJson.get("code"));
        log.info("--------------------------" + replace);
        jsonObject.put("content", replace);
        String post = HttpClientUtils.post(url, JSON.toJSONString(jsonObject), "application/json", "UTF-8", 10000, 10000);
        JSONObject jsonObject1 = JSONObject.parseObject(post);
        Object code = jsonObject1.get("code");
        if (!code.equals(1)) {
            return false;
        }
        return true;
    }

    //添加签名
    public static JSONObject addS(String sign) throws Exception {
        String url = MassageConfig.addSign.replace("{0}", MassageConfig.ddyApiKey).replace("{1}", sign);
        String get = HttpClientUtils.get(url);
        JSONObject resultBody = JSONObject.parseObject(get);
        //code=17  则是已经存在签名模板
        if (!resultBody.get("code").equals(1)) {
            return null;
        }
        return resultBody;
    }

    //查询签名
    public static JSONObject getS(String signIds) throws Exception {
        String url = MassageConfig.getSign.replace("{0}", MassageConfig.ddyApiKey).replace("{1}", signIds);
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("apikey", MassageConfig.ddyApiKey);
//        jsonObject.put("sign_ids", signIds);
        String get = HttpClientUtils.get(url);
        JSONObject resultBody = JSONObject.parseObject(get);
        if (!resultBody.get("code").equals(1)) {
            return null;
        }
//        Object check_status = resultBody.getJSONArray("result").getJSONObject(0).get("check_status");
//        log.info("------------------短信状态check_status：" + check_status);
        return resultBody;
    }

    //删除签名
    public static JSONObject delS(String signIds) throws Exception {
        String url = MassageConfig.delSign.replace("{0}", MassageConfig.ddyApiKey).replace("{1}", signIds);
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("apikey", MassageConfig.ddyApiKey);
//        jsonObject.put("sign_ids", signIds);
        String get = HttpClientUtils.get(url);
        JSONObject resultBody = JSONObject.parseObject(get);
        Object code = resultBody.get("code");
        if (!code.equals(104)) {
            return null;
        }
        return resultBody;
    }

    //添加模板
    public static JSONObject addT(String content, Integer type) throws Exception {
        String url = MassageConfig.addTemplate.replace("{0}", MassageConfig.ddyApiKey).replace("{1}", content).replace("{2}", String.valueOf(type));
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("apikey", MassageConfig.ddyApiKey);
//        jsonObject.put("content", content);
//        jsonObject.put("type", type);
        String get = HttpClientUtils.get(url);
        JSONObject jsonObject = JSONObject.parseObject(get);
        Object code = jsonObject.get("code");
        if (!code.equals(1)) {
            //111是模板已存在
            return null;
        }
        return jsonObject;
    }

    //查询模板
    public static JSONObject getT(String tplIds) throws Exception {
        String url = MassageConfig.getTemplate.replace("{0}", MassageConfig.ddyApiKey).replace("{1}", tplIds);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("apikey", MassageConfig.ddyApiKey);
//        jsonObject.put("sign_ids", signIds);
        String get = HttpClientUtils.get(url);
        JSONObject jsonObject = JSONObject.parseObject(get);

        Object code = jsonObject.get("code");

        if (!code.equals(1)) {
            return null;
        }
//        Object check_status = jsonObject.getJSONArray("result").getJSONObject(0).get("check_status");
//        log.info("------------------短信状态check_status：" + check_status);
        return jsonObject;
    }

    //删除
    public static JSONObject delT(String tplIds) throws Exception {
        String url = MassageConfig.delTemplate.replace("{0}", MassageConfig.ddyApiKey).replace("{1}", tplIds);
        String get = HttpClientUtils.get(url);
        JSONObject jsonObject = JSONObject.parseObject(get);
        Object code = jsonObject.get("code");
        if (!code.equals(106)) {
            return null;
        }

        return jsonObject;
    }

}

